package List.LinkList;

public class Test {
    public static void main(String[] args) {
        LinkList<String> list = new LinkList<>();
        list.insert("迪丽热巴");
        list.insert("古力娜扎");
        list.insert("马尔扎哈");
        list.insert(1, "小鸟丫丫");
        System.out.println(list.getMid());
        list.reverse();
        for (String s : list) {
            System.out.print(s + " ");
        }
        /*for(String s : list){
            System.out.print(s+" ");
        }
        System.out.println();
        System.out.println(list.get(3));
        System.out.println(list.remove(1));
        list.clear();
        System.out.println(list.isEmpty());
        System.out.println(list.length());*/

        //用于测试单链表是否有环测试用例
//        Node<String> first = new Node<>("aa", null);
//        Node<String> second = new Node<>("bb", null);
//        Node<String> third = new Node<>("cc", null);
//        Node<String> fourth = new Node<>("dd", null);
//        Node<String> fifth = new Node<>("ee", null);
//        Node<String> sixth = new Node<>("ff", null);
//        Node<String> seventh = new Node<>("gg", null);
//        first.next = second;
//        second.next = third;
//        third.next = fourth;
//        fourth.next = fifth;
//        fifth.next = sixth;
//        sixth.next = seventh;
//        seventh.next=third;//制造环
//        System.out.println(getEnterance(first).item);

    }

    private static class Node<T> {
        private T item;
        Node<T> next;

        public Node(T item, Node<T> next) {
            this.item = item;
            this.next = next;
        }
    }

    private static boolean isCircle(Node<String> n) {
        Node<String> fast = n;
        Node<String> slow = n;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast.equals(slow)) {
                return true;
            }
        }
        return false;
    }

    /*  查找环的入口节点
        当快慢指针相遇时定义一个新指针，Node temp = n;
        当 temp 和慢指针相遇时就是入口(不解释，涉及数论知识)
     */
    public static Node getEnterance(Node<String> n) {
        Node<String> fast = n;
        Node<String> slow = n;
        while (fast != null && fast.next != null) {//找环
            fast = fast.next.next;
            slow = slow.next;
            if (fast.equals(slow)) {//找到环
                Node<String> t = n;
                while (!t.equals(slow)) {
                    t = t.next;
                    slow = slow.next;
                }
                return t;
            }
        }
        /*Node<String> temp = null;
        while (fast != null && fast.next != null) {//找环
            fast = fast.next.next;
            slow = slow.next;
            if (fast.equals(slow)) {//找到环
                temp = n;
                continue;
            }
            if(temp != null){
                temp = temp.next;
                if(temp.equals(slow)){
                    return temp;
                }
            }
        }*/
        return null;
    }
}
